<?php
require_once(ROOTDIR."/include/class.rcon.php");
require_once(ROOTDIR."/include/database/class.BanQueries.php");
require_once(ROOTDIR."/include/database/class.ServerQueries.php");
require_once(ROOTDIR."/include/objects/class.Server.php");

// Get the hash value passed from ES
$hash = $_GET['hash'];

if($hash == $config->matchHash) {

  $steamId = $_GET['steamId'];
  $serverId = $_GET['serverId'];
  $nameOfBanned = $_GET['name'];
  $ipOfBanned = $_GET['ip'];
  
  // These are problem chars that need special conversion
  $nameOfBanned = convertAscii($nameOfBanned);
  
  // Trim IP of banned
  $ipOfBanned = explode(":", $ipOfBanned);
  $ipOfBanned = $ipOfBanned[0];
  
  $banQueries = new BanQueries();
  

  // Determine if this user is IP banned, if so kick them
  if($banQueries->isIpBanned($ipOfBanned)) {
    kickUser($steamId, $serverId, $config);
  }
       
  // Check to see if the user does exist in the ban list (if active)
  if($banQueries->doesUserExist($steamId)) {
    $now = time(); // Get the time now in seconds
    
  	$bannedUser = $banQueries->getBannedUserBySteamId($steamId);
  	
  	$length = $bannedUser->getLength()/60; // Convert to minutes

    // Pending bans are banned for 5 days
    if($bannedUser->getPending() == 1) {
      // Kick the user if the ban is 24 hours or less
      if($length > 0 && $length <= 24) {
        kickUser($steamId, $serverId, $config);
      } else {
        // Kick the user for the first 5 days that their ban is in pending mode
        $addDate = $bannedUser->getAddDate();
        
        $daysToKeepBanned = 24*3600*$config->daysBanPending;
        $expireDate = $addDate + $daysToKeepBanned;
        
        // Kick the user if it's still within 5 days of the pending ban add
        if($expireDate > $now) {
          kickUser($steamId, $serverId, $config);
        }
      }
    } else { // Handle non-pending bans normally
      // If length is 0, don't bother checking expire_date
      if($length == 0) {
        // Send rcon command to kick user
        kickUser($steamId, $serverId, $config);
      } else {
        // Check expire date to today's date
        if($bannedUser->getExpireDate() > $now) {
          kickUser($steamId, $serverId, $config);
        }
      }
    }
    
    // If their name is empty, update it
    if($bannedUser->getName() == "" || $bannedUser->getName() == null) {
      $banQueries->updateBanName($nameOfBanned, $steamId);
    }
    
    // Update their IP
    $banQueries->updateBanIp($ipOfBanned, $steamId);
    
  } // Make sure we found a user
} // End hash match

// Kick the user from the specified server
function kickUser($steamId, $serverId, $config) {

  $serverQueries = new ServerQueries();
  
  $server = $serverQueries->getServer($serverId);
    
  $r = new rcon($server->getIp(),$server->getPort(),$server->getRcon());
  $r->Auth();
  $r->kickUser($steamId, $config->banMessage);
}

function convertAscii($name) {
  $name = str_replace(":s1:", "'", $name);
  $name = str_replace(":s2:", "+", $name);
  $name = str_replace(":s3:", "?", $name);
  $name = str_replace(":s4:", "/", $name);
  $name = str_replace(":s5:", "&", $name);
  $name = str_replace(":s6:", "|", $name);
  $name = str_replace(":s7:", "%", $name);
  $name = str_replace(":s8:", "`", $name);
  $name = str_replace(":s9:", "#", $name);
  return $name;
}
?>
